<template>
  <div id="userRegisterView" class="login-container">
    <h2 class="login-title">用户注册</h2>

    <a-form
      class="login-form"
      label-align="left"
      auto-label-width
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="用户名">
        <a-input v-model="form.userAccount" placeholder="请输入用户名" />
      </a-form-item>

      <a-form-item field="userPassword" tooltip="密码不少于 8 位" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>

      <a-form-item field="checkPassword" label="确认密码">
        <a-input-password
          v-model="form.checkPassword"
          placeholder="请再次输入密码"
        />
      </a-form-item>

      <a-form-item>
        <a-button
          class="round-rect-btn login-button"
          type="primary"
          html-type="submit"
        >
          注册
        </a-button>
      </a-form-item>

      <!-- 新增：登录链接 -->
      <div class="login-link-container">
        <span>已有账号？</span>
        <router-link to="/user/login" class="login-link">立即登录</router-link>
      </div>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserRegisterRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
} as UserRegisterRequest);

const router = useRouter();

/**
 * 提交表单
 */
const handleSubmit = async () => {
  // 检查两次密码是否一致
  if (form.userPassword !== form.checkPassword) {
    message.error("两次输入的密码不一致");
    return;
  }

  try {
    const res = await UserControllerService.userRegisterUsingPost(form);

    if (res.code === 0) {
      message.success("注册成功，请登录");
      router.push({
        path: "/user/login",
        replace: false,
      });
    } else {
      message.error("注册失败，" + res.message);
    }
  } catch (error) {
    console.error("注册请求异常:", error);
    message.error("注册请求发生异常，请稍后重试");
  }
};
</script>

<style scoped>
/* 复用登录页面的样式 */
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  box-sizing: border-box;
}

.login-title {
  margin: 0 0 30px 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.login-form {
  width: 100%;
  max-width: 480px;
  padding: 30px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 圆角矩形按钮样式 */
.round-rect-btn {
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.login-button {
  width: 100%;
  min-width: 200px;
  height: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 按钮悬停效果 */
.round-rect-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

/* 登录链接样式 */
.login-link-container {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
}

.login-link {
  color: #165dff;
  text-decoration: none;
  margin-left: 4px;
}

.login-link:hover {
  text-decoration: underline;
}

/* 响应式适配 */
@media (max-width: 576px) {
  .login-form {
    padding: 20px;
  }

  .login-title {
    font-size: 20px;
  }
}
</style>
